package com.easy.service.impl;

import com.easy.bean.Department;
import com.easy.bean.Employee;
import com.easy.mapper.DepartmentDao;
import com.easy.mapper.EmployeeDao;
import com.easy.service.DepartmentService;
import com.easy.service.EmployeeService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeDao employeeDao;

    @Cacheable(value = "employee", key = "#id")
    @Override
    public Employee add(Employee employee){
        if (employee.getIsDeleted() == null) {
            employee.setIsDeleted((byte)0); // 默认未删除
        }
        employeeDao.add(employee);
        return employeeDao.one(employee.getId());
    }


    @Override
    public int del(Integer id) {
        return employeeDao.del(id);
    }


    @CachePut(value = "employee", key = "#employee.id")
    @Override
    public Employee edit(Employee employee) {
        employeeDao.edit(employee);
        Employee result=employeeDao.one(employee.getId());
        return result;
    }

    @Override
    public List<Employee> all(String name, String phone) {
        return employeeDao.all(name, phone);
    }


    @Override
    public Employee one(Integer id) {
        return employeeDao.one(id);
    }

    public void page(Page page, Employee employee){
        int count=employeeDao.pageCount(employee);
        Object data=employeeDao.page(page, employee);
        page.setCount(count);
        page.setData(data);
    }
}